package logic

import (
	"context"
	"fmt"
	"unicode/utf8"
	"zhihu/zhihurpc/zhihurpc"

	"zhihu/api/internal/svc"
	"zhihu/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AnswersAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAnswersAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AnswersAddLogic {
	return &AnswersAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AnswersAddLogic) AnswersAdd(req *types.AnswersAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	m := utf8.RuneCountInString(req.Content)
	if m > 100 {
		return nil, fmt.Errorf("评论字数不可以超过100个字符")
	}
	add, err := l.svcCtx.ZhihuRpc.AnswersAdd(l.ctx, &zhihurpc.AnswersAddRequest{
		Content:   req.Content,
		ProblemId: req.ProblemId,
		UserId:    req.UserId,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Message: "评论发布成功",
		Code:    200,
		Data:    add,
	}, nil
	return
}
